package com.example.mac.zyfypt_223yy.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.example.mac.zyfypt_223yy.R;
import com.example.mac.zyfypt_223yy.bean.LoginBean;
import com.example.mac.zyfypt_223yy.iface.LoginListener;
import com.example.mac.zyfypt_223yy.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener  {
    private TextView tv1, tvname,tvmima, tvhuanying, tvlogin;
    private Button btlogin, btreg;
    private EditText et1, et2;

    private Switch sw;
    private String username="",sessionid="",password="";//用户名密码
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:"+loginBean.getSessionid());
            if(loginBean.getSessionid()!=null) {
                sessionid=loginBean.getSessionid().toString();
                username=loginBean.getUsername();
                System.out.println("----username="+username);
                saveSP();
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        }
        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        //实例化sp
        sp=getSharedPreferences("login",MODE_PRIVATE);
        //读取sp
        readSP();
    }
    //读取sp
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        String session=sp.getString("sessionid",null);
        Boolean b=sp.getBoolean("remember",false);
        if (b)
        {
            et1.setText(name);
            et2.setText(pass);
            sw.setChecked(b);
        }
    }
    private void init() {
        tv1 = (TextView) findViewById(R.id.textView);
        tvhuanying = (TextView) findViewById(R.id.textView2);
        tvlogin = (TextView) findViewById(R.id.textView3);
        tvname = (TextView) findViewById(R.id.textView4);
        tvmima= (TextView) findViewById(R.id.textView11);
        btlogin=(Button)findViewById(R.id.button);
        btreg=(Button)findViewById(R.id.button2);
        et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        sw=(Switch)findViewById(R.id.switch1);
        btlogin.setOnClickListener(this);
        btreg.setOnClickListener(this);

    }
        @Override
        public void onClick(View view) {
            switch (view.getId())
            {
                case R.id.button:
                    //登录按钮操作
                    System.out.println("点击登陆按钮");
                    LoginModel loginModel = new LoginModel();
                    loginModel.getResult(username=et1.getText().toString(), password=et2.getText().toString(), loginListener);
                    saveSP();
                    break;
                case R.id.button2:
                    Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                    startActivity(intent1);
                    System.out.println("点击注册按钮");
                    break;
            }
        }
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString("sessionid",sessionid);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }
};




